﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DreamBuilder.Core;
using DreamBuilder.Common;
using DreamBuilder.Core.Container;
using ePlanet.ArchiveMange.Models;
using ePlanet.ArchiveMange.Services;
using System.Text;

namespace Web.Views.SingleWellManage
{
    /// <summary>
    /// SingleWellTreeHandle 的摘要说明
    /// </summary>
    public class SingleWellTreeHandle : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            string json = "[]";
            //获取文档类型
            if (context.Request.QueryString["archivemenu"] != null)
            {
                json = GetArchiveMenuData();
            }
            context.Response.ContentType = "text/json";
            context.Response.Write(json);
        }

        public bool IsReusable
        {
            get
            {
                return true;
            }
        }

        public string GetArchiveMenuData()
        {
            IArchiveMenuService IArchiveMenu = UContainer.Resolve<IArchiveMenuService>();
            ArchiveMangeModal.ARCHIVEMENUDataTable dtArchiveMenu =
                IArchiveMenu.GetAllArchiveMenu();
            dtArchiveMenu.Rows.Add("0", "文档类型", "", "-1");
            string jsonFormat = "{{\"id\": \"{0}\", \"text\": \"{1}\",\"pid\": \"{2}\", \"iconCls\": \"{3}\",  \"state\": \"{4}\",\"attributes\":{{\"typeCode\": \"{5}\"}}}},";
            StringBuilder resultStr = new StringBuilder("[");
            string iconCss = string.Empty;
            string ParentID = string.Empty;
            int i=1;
            foreach (ArchiveMangeModal.ARCHIVEMENURow row in dtArchiveMenu.Rows)
            {
                if (string.IsNullOrEmpty(row.PARENTID.ToString()))
                {
                    ParentID = "0";
                }
                else
                {
                    ParentID = row.PARENTID.ToString();
                }
                iconCss = ParentID == "-1" ? "icon-type" : "icon-folder";
                resultStr.Append(string.Format(jsonFormat, row.TYPEID, "编目"+i.ToString(), row.PARENTID, iconCss, "open", row.TYPECODE));
                i++;
            }
            if (resultStr.Length > 1)
            {
                resultStr = resultStr.Remove(resultStr.Length - 1, 1);
            }
            resultStr.Append("]");
            return resultStr.ToString();
        }
    }
}